
#ifndef	__CONFAB_AUCAB_HPP_2010__
#define	__CONFAB_AUCAB_HPP_2010__

#include <sys/types.h>
#include <map>
#include <string>

#include <db_cxx.h>

#include "aubin.hpp"


namespace   confab   {

class   Aucab
{
    typedef int32_t   cid_type;

public  :
    Aucab( const std::string env_home, const std::string db_home, const std::string data_root, u_int32_t n_tables=16 );
    ~Aucab();

public  :
    // insert record, or update it if the key exists
    int insert( cid_type   cid,
             const std::string& kpin,
             const std::string& key,
             const std::string& data );

    // insert record, or ignore it if the key exists
    int insertIgnore( cid_type   cid,
             const std::string& kpin,
             const std::string& key,
             const std::string& data );

    int find( cid_type   cid,
             const std::string& kpin,
             const std::string& key,
                   std::string& data );

    int erase( cid_type cid,
               const std::string& kpin,
               const std::string& key );

private	:
	void	initialize();
	void	finalize();

	void	createDefaultDbConfig( const std::string filename );


	void	recoverDbEnv( const std::string envhome );
	void	openDbEnv( const std::string envhome );
	void	closeDbEnv();

private :
    typedef Aubin*      aubin_ptr;
    typedef std::map< cid_type, aubin_ptr >   bins_type;
    typedef bins_type::iterator bins_iterator_type;

    bins_type  _bins;

    aubin_ptr   getBin( cid_type cid );
    std::string   getDataRoot( cid_type cid );
    std::string   getDataIndexFile( cid_type cid );

private :
	std::string	_env_home;
	std::string	_db_home;
    std::string _data_root;
	u_int32_t	_n_tables;

private	:
	DbEnv*	dbenv_p;

	u_int32_t	_count_put_or_erase;
};  //// class Aucab


}   //// namespace confab


#endif	////	__CONFAB_AUCAB_HPP_2010__

